﻿//////////////////////////////////////////////////////
//
//  This project makes use of Alex Gorbatchev's SyntaxHighlighter project
//      located at http://alexgorbatchev.com/SyntaxHighlighter/
//
//  It also refers to the hosted files on his servers.
//
//////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;

namespace Softlanding.SyntaxHighlighter
{
    public class SyntaxHighlighterDelegate : WebControl
    {
        /// <summary>
        /// On control load.  Initialize lists of scripts and styles
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        /// <summary>
        /// Simply overrides base class render, so no modification happens 
        ///     to what we're writing to the page
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            RenderContents(writer);
        }

        /// <summary>
        /// Overrides base class so no <span> gets added at beginning
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("");
        }

        /// <summary>
        /// Overrides base class so no </span> gets added at end
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("");
        } 

        /// <summary>
        /// We inject all client-side code and controls here.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (SPContext.Current.FormContext.FormMode != SPControlMode.Edit &&
                SPContext.Current.FormContext.FormMode != SPControlMode.New)
            {
                CssLink coreCss = new CssLink();
                coreCss.DefaultUrl = "http://alexgorbatchev.com/pub/sh/current/styles/shCore.css"; // in tutorial was shThemeDefault.css
                this.Controls.Add(coreCss);

                CssLink coreDefaultCss = new CssLink();
                coreDefaultCss.DefaultUrl = "http://alexgorbatchev.com/pub/sh/current/styles/shCoreDefault.css";
                this.Controls.Add(coreDefaultCss);

                //TODO: can we use ScriptLink objects instead using external urls?
                this.Page.ClientScript.RegisterClientScriptInclude("shCore",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shAutoloader",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js");


                //Load all brushes for languages that have been added to the SharePoint ribbon UI
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushCSharp",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushJScript",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushCpp",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushCss",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushJava",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushPerl",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushPhp",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushPlain",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushPowerShell",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPowerShell.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushPython",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushSql",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushVb",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js");
                this.Page.ClientScript.RegisterClientScriptInclude("shBrushXml",
                    "http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js");

                //lastly, load our file that contains any of our own javascript related to rendering syntax highlighting
                this.Page.ClientScript.RegisterClientScriptInclude("slsyntaxrenderonly",
                    "/_layouts/Softlanding.SyntaxHighlighter/SLSyntaxHighlightRenderOnly.js");
            }
            else
            {
                ScriptLink editOnlyScript = new ScriptLink();
                editOnlyScript.Name = "/_layouts/Softlanding.SyntaxHighlighter/SLSyntaxHighlightEditOnly.js";
                editOnlyScript.Language = "javascript";
                editOnlyScript.Localizable = false;
                this.Controls.Add(editOnlyScript);
            }
        }
    }
}
